package com.xjgc.eip.twice.zxjeip2cccjc.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.jeecg.modules.online.config.exception.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.xjgc.common.constant.Consants;
import com.xjgc.eip.twice.zxjeip2cccjc.entity.ZxjEip2cCcjc;
import com.xjgc.eip.twice.zxjeip2cccjc.mapper.ZxjEip2cCcjcMapper;
import com.xjgc.eip.twice.zxjeip2cccjc.service.ZxjEip2cCcjcService;

/**
 * <p>
 *  出厂检测 服务实现类
 * </p>
 *
 * @author wxy
 * @since 2019-11-11
 */
@Service
@DS("proxyDs")
public class ZxjEip2cCcjcServiceImpl extends ServiceImpl<ZxjEip2cCcjcMapper, ZxjEip2cCcjc> implements ZxjEip2cCcjcService {

    @Autowired
    ZxjEip2cCcjcMapper zxjEip2cCcjcMapper;

    /**
     * 批量插入审核通过的出厂检测数据到出厂检测表中
     * @param zxjEip2cCcjcs
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean reviewCcjc(List<ZxjEip2cCcjc> zxjEip2cCcjcs) throws BusinessException {
        boolean flag=false;
        if (zxjEip2cCcjcs!=null&&zxjEip2cCcjcs.size()>0){
        //根据选择的数据更新装置检测表的对应数据状态为2 标示"出厂检测已审核"
        List<String> capNos= Lists.newArrayList();
        for (ZxjEip2cCcjc zxjEip2cCcjco:zxjEip2cCcjcs){
            if (zxjEip2cCcjco!=null&& StringUtils.isNotBlank(zxjEip2cCcjco.getProductNo())){
                zxjEip2cCcjcMapper.reviewZzjcByCabNo(zxjEip2cCcjco);
                capNos.add(zxjEip2cCcjco.getProductNo());
            }
        }
        //反查出装置检测信息
        //存放审核通过的出厂检测信息
        List<ZxjEip2cCcjc> result= Lists.newArrayList();
        List<ZxjEip2cCcjc> zxjEip2cCcjcList=zxjEip2cCcjcMapper.selectZzjcListOver(zxjEip2cCcjcs);
        result.addAll(zxjEip2cCcjcList);
        //插入审核通过的装置检测数据到出厂检测表
        if (result.size()>0){
            List<ZxjEip2cCcjc> data=Lists.newArrayList();
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
            for (ZxjEip2cCcjc zxjEip2cCcjc:zxjEip2cCcjcs){
                if (zxjEip2cCcjc!=null&& StringUtils.isNotBlank(zxjEip2cCcjc.getProductNo())){
                    result.forEach(item -> {
                        if (zxjEip2cCcjc.getProductNo().equals(item.getProductNo())){
                            zxjEip2cCcjc.setProductOrdNo(item.getProductOrdNo());//循环反查的装置检测 填充出厂检测对应的生产订单号
                        }
                    });
                    zxjEip2cCcjc.setIoflag("1");
                    zxjEip2cCcjc.setReviewStatus(Consants.REVIEW_STATUS2);
                    zxjEip2cCcjc.setEndTime(new Date());
                    zxjEip2cCcjc.setInsulationTestRes("合格");
                    zxjEip2cCcjc.setPressureTestRes("合格");
                    zxjEip2cCcjc.setCurrentPreRangeRes("合格");
                    zxjEip2cCcjc.setDcSamRes("合格");
                    zxjEip2cCcjc.setSamVoltagePhaseRes("合格");
                    zxjEip2cCcjc.setSamCurrenAmpRes("合格");
                    zxjEip2cCcjc.setSamCurrenPhaseRes("合格");
                    zxjEip2cCcjc.setOpenInCircuitRes("合格");
                    zxjEip2cCcjc.setOpenOutCircuitRes("合格");
                    zxjEip2cCcjc.setFunctionTestRes("合格");
                    zxjEip2cCcjc.setFixedError("合格");
                    zxjEip2cCcjc.setWholeActTime(sdf.format(new Date()));
                    zxjEip2cCcjc.setHigPowIntReaTes("合格");
                    zxjEip2cCcjc.setSamVoltageAmpRes("合格");
                    zxjEip2cCcjc.setReturnTime(sdf.format(new Date()));
                    zxjEip2cCcjc.setProcessRouteNo(Consants.CCJC);
                    data.add(zxjEip2cCcjc);
                }
            }
            if (data!=null&&data.size()>0){
                zxjEip2cCcjcMapper.insertCcjc(data);
                //super.saveBatch(data,1000);
                flag=true;
            }
        }else {
            throw new BusinessException("出厂检测审核操作数据异常：屏号="+capNos.toString());
        }

        }
        return flag;
    }

    @Override
    public IPage<ZxjEip2cCcjc> selectReviewCcjcListPage(IPage<ZxjEip2cCcjc> page, ZxjEip2cCcjc zxjEip2cCcjc) {
        return zxjEip2cCcjcMapper.selectReviewCcjcListPage(page,zxjEip2cCcjc);
    }


}
